package com.zx.cloud.file.service.impl;

import com.zx.cloud.common.config.FileProperties;
import com.zx.cloud.file.service.WordService;
import com.zx.cloud.file.service.dto.FileDto;
import com.zx.cloud.file.util.WordUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;

/**
 * @program: zx-file-service
 * @description: word文件服务
 * @author: chenxf
 * @create: 2022-01-28 13:55
 **/
@Service
public class WordServiceImpl implements WordService {
    @Value("${file.uri}")
    private String fileUri;
    private final FileProperties properties;
    public WordServiceImpl(   FileProperties properties) {
        this.properties = properties;
    }
    @Override
    public FileDto saveWordFile(Map<String, String> replaceMap) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");

        String templatePath=replaceMap.get("templatePath");
        LocalDate sDate=LocalDate.now(),eDate=sDate.plusYears(2),signDate=sDate;
        Long leaseTerm= ChronoUnit.YEARS.between(sDate,eDate.plusDays(1));
        replaceMap.put("custName","***科技创新服务有限公司");
        replaceMap.put("address","**高新区育才路188路");
        replaceMap.put("rentArea","600");
        replaceMap.put("leaseTerm",leaseTerm.toString());
        replaceMap.put("sYear",sDate.getYear()+"");
        replaceMap.put("sMonth",sDate.getMonthValue()+"");
        replaceMap.put("sDay",sDate.getDayOfMonth()+"");
        replaceMap.put("eYear",eDate.getYear()+"");
        replaceMap.put("eMonth",eDate.getMonthValue()+"");
        replaceMap.put("eDay",eDate.getDayOfMonth()+"");
        replaceMap.put("signYear",signDate.getYear()+"");
        replaceMap.put("signMonth",signDate.getMonthValue()+"");
        replaceMap.put("signDay",signDate.getDayOfMonth()+"");
        replaceMap.put("yearMoney","10000");
        replaceMap.put("marginMoney","5000");
        String suffix = templatePath.substring(templatePath.indexOf('.') + 1);
        String newWordPath=properties.getPath().getPath()+"-" + format.format(date)+"."+suffix;
        File file=WordUtil.saveWordFile(properties.getPath().getPath()+ templatePath,newWordPath,replaceMap);
        assert file != null;
        return new FileDto(fileUri, file.getName());
    }
}
